using Microsoft.AspNetCore.Mvc;
using MallSystembackend.Api.Repository;
using MallSystembackend.Api.Model;
using Microsoft.Extensions.Configuration;
using MallSystembackend.Api.ParamModel;
using System.Linq;
namespace MallSystembackend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class CustomerAddressController : ControllerBase
    {
        public IRepository<CustomerAddress> _customerAddress;

        public readonly IConfiguration _configuration;

        public CustomerAddressController(IRepository<CustomerAddress> customerAddress,
        IConfiguration configuration
        )
        {
            _configuration = configuration;
            _customerAddress = customerAddress;
        }

        //添加用户地址
        [HttpPost]
        public ActionResult Address(ParamCustomerAddress model)
        {
            var UserAddress = new CustomerAddress
            {
                address = model.address,
                UsersId = model.UsersId
            };
            _customerAddress.Insert(UserAddress);
            return Ok(new
            {
                Code = 1000,
                Data = UserAddress,
                Msg = "添加成功"
            });
        }
        //修改用户地址
        [HttpPut("{id}")]
        public ActionResult UpdateAddress(ParamCustomerAddress model, int id)
        {
            var UserAddress = _customerAddress.GetById(id);
            if (UserAddress != null)
            {
                UserAddress.address = model.address;
                UserAddress.UsersId = model.UsersId;
            }
            _customerAddress.Update(UserAddress);
            return Ok(new
            {
                Code = 1000,
                Data = UserAddress,
                Msg = "修改成功"
            });
        }
        //根据不同用户id获得对应的地址表
        [HttpGet("{id}")]
        public ActionResult GetAddress(int id)
        {
            var UserAddress = _customerAddress.Table;
            var GetOneUser = UserAddress.Where(x => x.UsersId == id).Where(x => x.IsDeleted == false);
            return Ok(new
            {
                Code = 1000,
                Data = GetOneUser,
                Msg = "获得成功"
            });
        }
        //删除地址
        [HttpPut("{id}")]
        public ActionResult DeleteAddress(int id)
        {
            var UserAddress = _customerAddress.GetById(id);
            if (UserAddress != null)
            {
                _customerAddress.Delete(UserAddress);
                return Ok(new
                {
                    Code = 1000,
                    Data = UserAddress,
                    Msg = "删除成功！！！"
                });
            }
            return Ok(new
            {
                Code = 1002,
                Data = " ",
                Msg = "操作失败"
            });


        }
    }
}